// pages/auth/auth.vue
<template>
    <view>
        <text>来到回调页面，获取授权码...</text>
    </view>
</template>

<script>
export default {
    onShow(options) {
        const { code } = options.query; // 获取授权码
        if (code) {
            this.requestAccessToken(code);
        }
    },
    methods: {
        requestAccessToken(code) {
            const { clientID, clientSecret, tokenUrl } = this.$config.OAUTH_CONFIG;

            uni.request({
                url: tokenUrl,
                method: 'POST',
                data: {
                    code: code,
                    client_id: clientID,
                    client_secret: clientSecret,
                    redirect_uri: this.$config.OAUTH_CONFIG.redirectURI,
                    grant_type: 'authorization_code'
                },
                success: (res) => {
                    if (res.data && res.data.access_token) {
                        // 存储访问令牌
                        uni.setStorageSync('accessToken', res.data.access_token);
                        // 跳转到主页或其他页面
                        uni.switchTab({ url: '/pages/home/home' });
                    } else {
                        uni.showToast({
                            title: '获取token失败',
                            icon: 'none'
                        });
                    }
                },
                fail: () => {
                    uni.showToast({
                        title: '请求token失败',
                        icon: 'none'
                    });
                }
            });
        }
    }
};
</script>
